package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.config.zhaoshang.ZsProjectInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogZsProjectOpen;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsConfigMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.S_ZS_PROJECT_OPEN_FROM_CROSS, desc = "承包项目")
public class ZsProjectOpenCmdFromCross extends AbstractCommand {

    private int templateId;
    private int num;
    private long projectId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsProjectOpenRespMsgFromCross respMsgFromCross = ZhaoShangProto.ZsProjectOpenRespMsgFromCross.parseFrom(packet.getBytes());
        ret = respMsgFromCross.getRet();
        templateId = respMsgFromCross.getTemplateId();
        num = respMsgFromCross.getNum();
        projectId = respMsgFromCross.getProjectId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ZsProjectInfo zsProjectInfo = ZsConfigMgr.getZsProjectInfoMap().get(templateId);
        if(zsProjectInfo == null){
            ret = GameErrorCode.E_ZS_PROJECT_NOT_FOUND;
        }

        Property consume = PropertyHelper.parseStringToProperty(zsProjectInfo.getConsume());
        for(int goodsId : consume.getGoods().keySet()){
            long goodsNum = consume.getCountByGoodsId(goodsId).longValue() * num;
            consume.setGood(goodsId, BigInteger.valueOf(goodsNum));
        }

        //还原预扣记录
        player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(consume));

        if(ret == 0){
            //扣除道具
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            currencyModule.removeCurrency(consume, eLogMoneyType.ZhaoShang, eLogMoneyType.ZsProjectOpenConsume);

            int decorationClass = GoodsMgr.getUserDecorationClass(player.getUserInfo().getDecoration());
            //立项日志
            AutoLogMgr.add(new LogZsProjectOpen(player.getUserId(), projectId, decorationClass, player.getTitleId(), templateId));
            player.notifyListener(eGamePlayerEventType.ZhaoshangCanyu.getValue(),1);
        }

        ZhaoShangProto.ZsProjectOpenRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectOpenRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setTemplateId(templateId);
        respMsg.setProjectId(projectId);
        player.sendPacket(ClientProtocol.U_ZS_PROJECT_OPEN, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
